﻿# coding=UTF-8
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

from google.appengine.ext import db
#http://code.google.com/p/gae-services-examples/source/checkout
import os
from google.appengine.ext.webapp import template

g_categories = [u"Антикварная вещица", u"Естонский товар", u"Грузинский товар"]
class Product(db.Model):
	name = db.StringProperty(required=True)
	description = db.StringProperty(multiline=True)
	price = db.FloatProperty()
	date = db.DateTimeProperty(auto_now_add=True)
	rating = db.RatingProperty()
	count = db.IntegerProperty()
	category = db.StringProperty(choices=set(g_categories))#db.ReferenceProperty(ItemCategory)
	image_path = db.StringProperty()
	lang = "rus"
	
	def name_loc(self):
		return self.lang
	
class LocalizationLabel(db.Model):
	name = db.StringProperty(required=True)
	
class LocalizationLang(db.Model):
	name = db.StringProperty(required=True)
	description = db.StringProperty(required=True)
	
class LocalizationTranslation:
	label = db.ReferenceProperty(LocalizationLabel)
	lang = db.ReferenceProperty(LocalizationLang)
	translation = db.StringProperty(required=True)
	

def createProducts():
	flushModel(Product)
	return [createProduct(u"Туфли охуенные", 2, 15.34, 5, 'superboots.gif'), 
			createProduct(u"Вилочка золотая", 2, 2.00, 5, 'goldenfork.gif'),
			createProduct(u"Камуфляж грузинский", 2, 20.50, 5, 'gshirt.gif'),
			createProduct(u"Джып жыдовский", 2, 12000.01, 3, 'jeep.gif'),
			createProduct(u"Напиток спрайт", 1, 0.55, 3),
			createProduct(u"Прокладкі Олвейс", 1, 0.55, 3),
			]
			
def flushModel(modelClass):
	for entity in modelClass.all():
		entity.delete()
		
def createTranslations():
	flushModel(LocalizationLabels)
	flushModel(LocalizationLangs)
	flushModel(LocalizationTranslation)

def createProduct(name, category_id, price, rating, image=''):
	product = Product(name=name,
					category=g_categories[category_id],
					price = price,
					rating = rating,
					image_path = image
					)
	product.put()
	return product



class MainPage(webapp.RequestHandler):

	def get(self, lang='rus'):
		self.LANG = lang
		template_values = {
			'products': Product.all().order('category'),
			'topProducts': Product.all().order('-rating').fetch(3),
			}

		path = os.path.join(os.path.dirname(__file__), 'templates/index.htm')
		self.response.out.write(template.render(path, template_values))
		#

application = webapp.WSGIApplication(
									[('/', MainPage),
									('/(\w{3})/?$', MainPage)
									],
									debug=True)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()